// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.BodyDamage.BodyPart;
import zombie.characters.BodyDamage.BodyPartType;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.network.packets.INetworkPacket;

public class PlayerBodyPart implements INetworkPacket {
    protected byte bodyPartIndex;
    protected BodyPart bodyPart;

    public void set(BodyPart _bodyPart) {
        if (_bodyPart == null) {
            this.bodyPartIndex = -1;
        } else {
            this.bodyPartIndex = (byte)_bodyPart.getIndex();
        }

        this.bodyPart = _bodyPart;
    }

    public void parse(ByteBuffer b, IsoGameCharacter character) {
        boolean _boolean = b.get() == 1;
        if (_boolean) {
            this.bodyPartIndex = b.get();
            if (character == null) {
                this.bodyPart = null;
            } else {
                this.bodyPart = character.getBodyDamage().getBodyPart(BodyPartType.FromIndex(this.bodyPartIndex));
            }
        } else {
            this.bodyPart = null;
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        DebugLog.Multiplayer.error("PlayerBodyPart.parse is not implemented");
    }

    @Override
    public void write(ByteBufferWriter b) {
        if (this.bodyPart == null) {
            b.putByte((byte)0);
        } else {
            b.putByte((byte)1);
            b.putByte((byte)this.bodyPart.getIndex());
        }
    }

    @Override
    public String getDescription() {
        return "\n\tPlayerBodyPart [ Item=" + (this.bodyPart == null ? "?" : "\"" + this.bodyPart.getType().name() + "\"") + " ]";
    }

    public BodyPart getBodyPart() {
        return this.bodyPart;
    }
}
